# 获取时间

from rest_framework.response import Response
from rest_framework.views import APIView
from datetime import date, timedelta
# from goods.models import GoodsVisitCount
from goods.models import GoodsVisitCount
from users.models import User
from rest_framework.permissions import IsAdminUser


# ------------------获取用户总数---------

class UserToalCount(APIView):
    # 权限指定
    permission_classes = [IsAdminUser]
    def get(self,request):
        # 查询用户表数据获取所有注册用户数据
        count = User.objects.filter(is_staff=False).count()
        return Response({
            'count':count,
            'data':date.today()
        })
#-----------------获取日增用户-------------
class UserDayCount(APIView):
    # 权限指定
    permission_classes = [IsAdminUser]
    def get(self,request):
        # 获取当天日期
        now_date = date.today()
        # 根据日期查询注册用户
        count = User.objects.filter(date_joined__gte=now_date,is_staff=False).count()
        return Response({
            'count':count,
            'date':now_date
        })
# ------------------日增用户----------------
class UserDayActiveCount(APIView):
    # 特权指定
    permission_classes = [IsAdminUser]
    def get(self,request):
        # 获取当天日期
        now_date = date.today()
        # 根据日期查询登录用户
        count= User.objects.filter(last_login__gte=now_date,is_staff=False).count()
        return Response({
            'count':count,
            'date':now_date
        })
# ------------------下单用户统计-----------------
class UserDayOrdersCount(APIView):
    permission_classes = [IsAdminUser]
    def get(self,request):
        # 获取当天日期
        now_date = date.today()
        # 根据日期查询下单用户
        user = set(User.objects.filter(orderinfo__create_time__gte=now_date,is_staff=False))
        count = len(user)
        print(count)
        return Response({
            'count':count,
            'date':now_date
        })
# ------------------获取月增用户-----------------
class UserMonthCOunt(APIView):
    # 权限指定
    permission_classes = [IsAdminUser]

    def get(self,request):
        # 获取当天时间
        now_date = date.today()
        # 获取30天前日期
        old_date = now_date-timedelta(days=30)
        # 遍历30天范围内每天的注册用户数量，每当遍历一天添加到列表中
        date_list = []
        for i in range(31):
            # 获取当前日期
            index_date = old_date + timedelta(i)
            # 获取下一天文日期
            next_date = old_date+timedelta(i+1)
            count  = User.objects.filter(date_joined__gte=index_date,date_joined__lt=next_date,is_staff=False).count()
            date_list.append({
                'count':count,
                'date':index_date
            })
        return Response(date_list)
    #------------获取商品分类---------------
class GoodsTypeCount(APIView):
    permission_classes = [IsAdminUser]
    def get(self,request):
        # 获取当前天数
        now_date = date.today()
        # 查询商品分类访问量表
        goodscount = GoodsVisitCount.objects.filter(date=now_date)
        # 获取数量
        data_list = []
        for goods in goodscount:
            data_list.append({
                'count':goods.count,
                'category':goods.category.name
            })
        return Response(data_list)


